#include <iostream>
#include <vector>
using namespace std;

vector <int> crear();
void mostrar(vector <int> array);
int posicion(vector <int> array, int li, int ls);
int comprobar(vector <int> array);

vector <int> crear(){

	int num,i,element;
    vector <int> vec;

	cout << "\nIntroduzca el numero de elementos del vector. " << endl;
	cin >> num;

	for(i=0; i<num; i++){
		cout << "\nIntroduzca el elemento " << i+1 << endl;
		cin >> element;

		vec.push_back(element);
	}

	return vec;
}

void mostrar(vector <int> array){	
	
	for (int i=0; i < array.size(); i++) cout << array[i] << ' ';
	cout << '\n';	

}

int comprobar(vector <int> array){

	return posicion(array,0,array.size());
}

int posicion(vector <int> array, int li, int ls){

	int solucion=-1;

	//Caso Base
	if(li==ls && array[li]==li){
		solucion=array[li];
	}
	else{ //Caso general
		int mitad=(li+ls)/2;
		if(li<ls){
			if(mitad==array[mitad]){
				solucion=array[mitad];
			}else if (mitad > array[mitad]){
				solucion=posicion(array,mitad+1,ls);
			}else{
				solucion=posicion(array,li,mitad-1);
			}
		}
	}
	return solucion;
}

int main(){

	vector <int> array=crear();

	cout << "\nVector: ";
	mostrar(array);

	int sol=comprobar(array);

	cout << "La posicion correcta es: " << sol << endl << endl;
}
